#!/usr/bin/env python

import sys
import os
import shutil
import re

class SvnMock(object):

    BASE_DIR = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

    def execute(self):
        command = sys.argv[1]
        if command == 'export':
            revision = sys.argv[3]
            source = sys.argv[4]
            destination = sys.argv[5]

            if source.endswith('config/all/'):
                path = os.path.join(self.BASE_DIR, 'testdata/revs', revision, 'all')
                self.copySubTree(path, destination)

            reg = re.match('.*config/(loc|typ|loctyp|host)/([a-z0-9]+)/?', source)
            if reg:
                path = os.path.join(self.BASE_DIR, 'testdata/revs', revision, reg.group(2))
                self.copySubTree(path, destination)


            if source.endswith('is24-config.spec'):
                spec_template = os.path.join(self.BASE_DIR, 'testdata/misc/is24-config.spec')
                print "A   %s" % destination
                shutil.copy(spec_template, destination)

    def copySubTree(self, source, destination):
        if os.path.exists(source):
            for root, dirs, files in os.walk(source):
                relpath = os.path.relpath(root, source)
                for d in dirs:
                    path = os.path.join(destination, relpath, d)
                    if not os.path.exists(path):
                        print "A   %s" % path
                        os.mkdir(path)

                for f in files:
                    path = os.path.join(destination, relpath, f)
                    print "A   %s" % path
                    shutil.copy(os.path.join(root, f), path)


SvnMock().execute()


